﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace ClientLib
{
    public class ErrorUtil
    {
        public ErrorUtil()
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cdata"></param>
        /// <param name="errmsg"></param>
        public void LogAppError(string userid, string methodname, string parameters, string errmsg)
        {
            try
            {
                WriteToEventLog(ConcatenateMessage(userid, methodname, parameters, errmsg));
            }
            catch (Exception ex)
            {
                // write to text log file (unable to write to eventviewer)

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cdata"></param>
        /// <param name="exc"></param>
        public void LogException(string userid, string methodname, string parameters, Exception exc)
        {
            try
            {
                string errmsg = GetExceptionInfo(exc);
                WriteToEventLog(ConcatenateMessage(userid, methodname, parameters, errmsg));
            }
            catch (Exception ex)
            {
                // write to text log file (unable to write to eventviewer)

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="emsg"></param>
        public void WriteToEventLog(string emsg)
        {
            try
            {
                System.Diagnostics.EventLog TestApp = new EventLog();
                if (!System.Diagnostics.EventLog.SourceExists("TestApp")) System.Diagnostics.EventLog.CreateEventSource("TestApp", "Application");
                TestApp.Source = "TestApp";
                TestApp.WriteEntry(emsg);
            }
            catch (Exception ex)
            {
                // write to text log file (unable to write to eventviewer)

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        private string GetExceptionInfo(Exception ex)
        {
            string errmsg = "\n\n--------------------";
            errmsg += "\nError Type: " + ex.GetType().Name.ToString();
            errmsg += "\nError Source: " + ex.Source.ToString();
            errmsg += "\nError Target: " + ex.TargetSite.ToString();
            errmsg += "\nError Message: " + ex.Message.ToString();
            errmsg += "\nError Stack: " + ex.StackTrace.ToString();
            return errmsg;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="mname"></param>
        /// <param name="mparams"></param>
        /// <param name="emsg"></param>
        /// <returns></returns>
        private string ConcatenateMessage(string uid, string mname, string mparams, string emsg)
        {
            string concatStr = "\nUserID: " + uid;
            concatStr += "\nMethodName: " + mname;
            concatStr += "\nMethodParameters: " + mparams;
            concatStr += "\nErrorMessage: " + emsg;
            return concatStr;
        }
    }
}
